package com.office.web;

import com.office.bean.TMajor;
import com.office.bean.TmajorType;
import com.office.service.MajorService;
import com.office.utils.DepartBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class MajorControl {
    @Autowired
    MajorService majorService;
    @RequestMapping("/Major_list")
    private String toList(long pageIndex, long pagesize, Model model){

        System.out.println("这是course的分页数据"+"   "+"初始页面:"+pageIndex+"页面大小："+pagesize);
        Map<String,Object> map = new HashMap<>();
        map.put("pageIndex",(pageIndex-1)*pagesize);//放入起始页
        map.put("pagesize",pagesize);//放入页面大小

        //查询总记录
        int count = majorService.findEmpusercount();
        //查询分页的记录
        List<TMajor> majorList = majorService.findMajorBypage(map);
        System.out.println("查询到的major有："+majorList);
        //放入到major工具类
        DepartBeanUtils<TMajor> majorutils = new DepartBeanUtils<>(pageIndex, pagesize, count, majorList);

        model.addAttribute("majorutils",majorutils);
        return "courselist";
    }
    @RequestMapping("/Major_add")
    public void addMajor(TMajor tMajor, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        System.out.println("（专业新增）新增的专业清单为："+tMajor);
        int i = majorService.addMajor(tMajor);
        System.out.println(i);
        if (i>0){
//            resp.getWriter().write("<script>alert('新增成功');location.href='/xxxxx'</script>");
            resp.getWriter().write("<script>alert('学科新增成功!');location.href='/Major_list?pageIndex=1&pagesize=5'</script>");
        }else {
            resp.getWriter().write("<script>alert('学科新增失败!');location.href='/Major_add</script>");

        }
    }
    @RequestMapping("/Major_findTypeMajor")
    @ResponseBody
    public List<TmajorType> findTypeMajor( ){
       return majorService.findMajorType();
    }

    @RequestMapping("/Major_delect/{id}")
    @ResponseBody
    public boolean delect(@PathVariable("id") int id){
        boolean b = majorService.delectMajor(id);
        System.out.println("学科删除"+b);
        return b;
    }

    @RequestMapping("/Major_toupdate/{id}")
    public String toUpdata(@PathVariable int id,Model model){
        System.out.println("要编辑的学科的id为："+id);
        TMajor tmajor = majorService.findmajorbyid(id);
        model.addAttribute("Major",tmajor);
        System.out.println("查询到的专业为："+tmajor);
        return "courseupdate";
    }
    @RequestMapping("/Major_update")
//    @ResponseBody
    public void UpdateMajor(TMajor tMajor,HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        System.out.println("要修改的专业为"+tMajor);
        int i = majorService.updateMajor(tMajor);
        if (i>0){
//            resp.getWriter().write("<script>alert('学科新增成功!');location.href='/Major_list?pageIndex=1&pagesize=5'</script>");
            resp.getWriter().write("<script>alert('更新成功！正在跳转');location.href='/Major_list?pageIndex=1&pagesize=5'</script>");
        }else {
            resp.getWriter().write("<script>alert('更新失败！正在跳转';location.href='/Major_update')</script>");
        }
    }
}
